package Login;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Login {

    private static String loginName;
    private static String loginPasswd;
    private static Connection verbindung = null;
    final private static String hostname = "localhost";
    final private static String port = "3306";
    final private static String dbname = "test";
    final private static String dbUser = "root";
    final private static String dbPassword = "test";

    public static String getName() {
        return loginName;
    }

    public static void setName(String name) {
        loginName = name;
    }

    public static String getPasswd() {
        return loginPasswd;
    }

    public static void setPasswd(String passwd) {
        loginPasswd = passwd;
    }

    public static boolean login() {
        if (Login.isMySQLServerOnline()) {
            ResultSet rs = null;
            try {
                rs = verbindung.createStatement().executeQuery("SELECT passwort FROM benutzer WHERE name=" + "'" + Login.getName() + "'");
                while (rs.next()) {
                    if (rs.getString("passwort").equals(Login.getPasswd())) {
                        return true;
                    } else {
                        return false;
                    }
                }
                rs.close();
                verbindung.close();
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    public static boolean register() {
        if (Login.isMySQLServerOnline()) {
            String sql = "INSERT INTO benutzer (name, passwort) VALUES('" + Login.getName() + "', " + "'" + Login.getPasswd() + "')";
            try {
                if (Login.isUserAlreadyInDatabase()) {
                    verbindung.close();
                    return false;
                } else {
                    verbindung.createStatement().execute(sql);
                    verbindung.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    private static boolean isUserAlreadyInDatabase() {
        if (Login.isMySQLServerOnline()) {
            ResultSet rs = null;
            try {
                rs = verbindung.createStatement().executeQuery("SELECT name FROM benutzer WHERE name=" + "'" + Login.getName() + "'");
                while (rs.next()) {
                    if (rs.getString("name").equals(Login.getName())) {
                        return true;
                    } else {
                        return false;
                    }
                }
                rs.close();
                //verbindung.close();
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    private static boolean isMySQLServerOnline() {
        try {
            System.out.println("* Treiber laden");
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception e) {
            System.err.println("Unable to load driver.");
            e.printStackTrace();
            return false;
        }

        try {
            System.out.println("* Verbindung aufbauen");
            String url = "jdbc:mysql://" + hostname + ":" + port + "/" + dbname;
            verbindung = DriverManager.getConnection(url, dbUser, dbPassword);

            System.out.println("* Datenbank-Verbindung beenden");
            //verbindung.close();
            return true;
        } catch (SQLException sqle) {
            System.out.println("SQLException: " + sqle.getMessage());
            System.out.println("SQLState: " + sqle.getSQLState());
            System.out.println("VendorError: " + sqle.getErrorCode());
            sqle.printStackTrace();
            return false;
        }
    }
}
